﻿using System;
using System.Windows.Forms;
using FoundationClass;
using FoundationClass.Entity;
using Telerik.WinControls.UI;

namespace P2MARY_INTERNET.Controls
{
    public partial class ComputerForm : UserControl
    {
        public ComputerForm()
        {
            InitializeComponent();
            InitializeComputerData();
            InitializeControl();
        }

        private void InitializeControl()
        {
            pnlCompData.Enabled = btnDeleteCom.Enabled = lstComputer.SelectedIndex > -1;
        }

        private void InitializeComputerData()
        {
            try
            {
                var computerList = MasterDataHandler.GetAllComputors();
                if (computerList == null)
                    return;

                drpComType.SelectedIndex = 0;
                foreach (var c in computerList)
                {
                    var com = new RadListDataItem(c.ComputerName, c);
                    if (c.Status == 'A')
                    {
                        com.Image = imageList1.Images[0];
                    }
                    else
                    {
                        com.Image = imageList1.Images[1];
                    }
                    lstComputer.Items.Add(com);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(@"InitializeComputerData : " + ex.Message + @" | " + ex.StackTrace);
            }
        }

        private void lstComputer_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            try
            {
                if (lstComputer.SelectedIndex == -1)
                {
                    ClearForm();
                    return;
                }
                pnlCompData.Enabled = btnDeleteCom.Enabled = lstComputer.SelectedIndex > -1;

                var computer = lstComputer.SelectedItem.Value as Computer;
                if (computer == null) return;

                txtComName.Text = computer.ComputerName;
                txtComDetails.Text = computer.Specification;
                if (computer.Status == 'A')
                {
                    drpComStatus.SelectedIndex = 0;
                    drpComStatus.Items[0].Selected = true;
                }
                else
                {
                    drpComStatus.SelectedIndex = 0;
                    drpComStatus.Items[1].Selected = true;
                }
                if (computer.ComputerType == 'I')
                {
                    drpComType.SelectedIndex = 0;
                    drpComType.Items[0].Selected = true;
                }
                else
                {
                    drpComType.SelectedIndex = 1;
                    drpComType.Items[1].Selected = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"lstComputer_SelectedIndexChanged :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void ClearForm()
        {
            txtComName.Text = string.Empty;
            txtComDetails.Text = string.Empty;
            drpComStatus.SelectedIndex = 0;
            drpComType.SelectedIndex = 0;
        }

        private void btnAddCom_Click(object sender, EventArgs e)
        {
            try
            {
                var dlg = new ComputerDialog { StartPosition = FormStartPosition.CenterParent };
                Computer computer;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    computer = dlg.Computer;
                    var radL = new RadListDataItem(computer.ComputerName, computer);
                    radL.Image = imageList1.Images[0];
                    lstComputer.Items.Add(radL);
                    MasterDataHandler.AddComputer(computer);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnAddCom :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeleteCom_Click(object sender, EventArgs e)
        {
            try
            {
                var computer = lstComputer.SelectedItem.Value as Computer;
                if (computer == null) return;

                var confirm = new ConfirmDialog {StartPosition = FormStartPosition.CenterParent};
                if (DialogResult.OK == confirm.ShowDialog())
                {
                    MasterDataHandler.RemoveComputer(computer.Id);
                    lstComputer.Items.Remove(lstComputer.SelectedItem);
                    btnAddCom.Enabled = btnDeleteCom.Enabled = lstComputer.Items.Count != 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnDeleteCom_Click :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSaveCom_Click(object sender, EventArgs e)
        {
            try
            {
                if(txtComName.Text.Length == 0 || txtComDetails.Text.Length == 0)
                {
                    lblValidate.Text = @"กรูณากรอกข้อมูลให้ครบ";
                    return;
                }
                var com = lstComputer.SelectedItem.Value as Computer;
                if (com == null)
                    return;

                com.ComputerName = txtComName.Text;
                com.Specification = txtComDetails.Text;
                if (drpComStatus.SelectedIndex == 0)
                {
                    com.Status = 'A';
                    lstComputer.SelectedItem.Image = imageList1.Images[0];
                }
                else
                {
                    com.Status = 'I';
                    lstComputer.SelectedItem.Image = imageList1.Images[1];
                }

                if (drpComType.SelectedIndex == 0)
                {
                    com.ComputerType = 'I';
                }
                else
                {
                    com.ComputerType = 'G';
                }

                lstComputer.SelectedItem.Value = com;
                lstComputer.SelectedItem.Text = com.ComputerName;
                MasterDataHandler.UpdateComputer(com);
                MessageBox.Show(@"บันทึกข้อมูลเรียบร้อย");
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnSaveCom_Click :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }
}
